package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.utils.QuerySplitUtil;
import org.springframework.util.StringUtils;

import com.bz.xtcx.manager.vo.VoQuery;

import java.util.List;

public class BusUserFormHisProvider {

	public String findByCondition(VoQuery e) {
		//StringBuilder sql = new StringBuilder("select * from `bus_user_form_his` f left join `bus_user` u on f.user_id=u.user_id where 1=1");
		StringBuilder sql = new StringBuilder("select f.id, f.res_code,f.user_id,f.name,f.code,f.name_alias,f.code_alias, ");
        sql.append(" f.re_name,f.form_type,f.detail,f.check_status,f.status,f.creater, ");
        sql.append(" f.create_time,f.updater,f.update_time,g.info,g.checker,g.check_time ");
        sql.append(" from `bus_user_form_his` f ");
        sql.append(" left join (select * from `bus_user_form_check` x where check_time =(select max(check_time) from `bus_user_form_check` y where x.form_id = y.form_id)) g on f.id = g.form_id where 1=1 ");
		sql.append(" and f.form_type = ").append(e.getUserType());
		sql.append(" and f.check_status = ").append(e.getCheckStatus());
		sql.append(queryCondition(e));
        return sql.toString();
	}
	
	StringBuilder queryCondition(VoQuery e) {
		StringBuilder sql = new StringBuilder();
		if(e != null) {
			if(e.getUserId() != null) {
				sql.append(" and f.user_id = '").append(e.getUserId()).append("'");
			}
			if (!StringUtils.isEmpty(e.getObjName())) {
				List<String> paramList = QuerySplitUtil.getParamList(e.getObjName());
				sql.append(" and (");
				for (int i = 0; i < paramList.size(); i++) {
					String param = paramList.get(i);
					if(i != 0){
						sql.append(" or ");
					}
					sql.append(" f.name like '%"+param+"%'");
					sql.append(" or f.code like '%"+param+"%'");
					sql.append(" or f.res_code like '%"+param+"%'");
					sql.append(" or f.detail like '%"+param+"%'");
				}
				sql.append(")");
			}
		}
		return sql;
	}
	
}
